package com.billdowney.qq.robot.util;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.io.*;
import java.nio.charset.StandardCharsets;

/**
 * @Author halon
 * @create 2021/9/
 */
public class ReadLocalJsonFileDemo {
//    public static void main(String[] args) throws IOException {
//        File file = new File("C:\\Users\\radar\\Desktop\\qianming\\76561198875391344.json");
//      Map<String,Object> map=  JSON.parseObject(readerMethod(file));//获取文件对象
//        System.out.println(map);
//        xieru(map);
//    }



    public static String readerMethod(File file) throws IOException {
        FileReader fileReader = new FileReader(file);
        Reader reader = new InputStreamReader(new FileInputStream(file), "Utf-8");
        int ch = 0;
        StringBuffer sb = new StringBuffer();
        while ((ch = reader.read()) != -1) {
            sb.append((char) ch);
        }
        fileReader.close();
        reader.close();
        String jsonStr = sb.toString();
//        System.out.println(JSON.parseObject(jsonStr));
        return jsonStr;
    }


    //写入
    public static void xieru(String Url,Object inputData) throws IOException {
        File file = new File(Url);
//file.createNewFile();
        if (!file.exists()) {
            //file.mkdir();
            try {
                //创建文件
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
//SerializerFeature.WriteMapNullValue
//上边参数含义为如果属性为空,也不省略该属性
// inputData 往文件中 写的数据
        Object obj = JSONObject.toJSONString(inputData, SerializerFeature.WriteMapNullValue);
        String json = obj.toString();

        Writer write = new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8);
        write.write(json);
        write.flush();
        write.close();

    }

}

